package com.crazypos.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazypos.constant.UserTypeConstant;
import com.crazypos.dao.OrganizationDao;
import com.crazypos.dao.UserDao;
import com.crazypos.exceptions.MyException;
import com.crazypos.pojo.OrganizationEntity;
import com.crazypos.pojo.UserEntity;
import com.crazypos.pojo.bo.AdminCreateRootOrganizationBO;
import com.crazypos.pojo.bo.AdminUpdateOrganizationBO;
import com.crazypos.pojo.bo.OrganizationCreateBO;
import com.crazypos.service.IOrganizationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrganizationService extends ServiceImpl<OrganizationDao, OrganizationEntity> implements IOrganizationService {

    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void createorganization(OrganizationCreateBO createBO) {
        OrganizationEntity entity = new OrganizationEntity();
//        entity.setId("123");
        entity.setName("123");
        entity.setAdminId("123");
        entity.setCreatedBy("123");
        entity.setParentId("0");
        organizationDao.insert(entity);
    }


    @Transactional(propagation = Propagation.REQUIRED)
    public void adminCreateOrganization(AdminCreateRootOrganizationBO bo, String createById) {
        OrganizationEntity entity = new OrganizationEntity();
        entity.setName(bo.getName());
        entity.setParentId("0");
        entity.setCreatedBy(createById);
        if (bo.getAdminId() != null) {
            entity.setAdminId(bo.getAdminId());
        }

        organizationDao.insert(entity);
    }

    @Override
    public IPage<OrganizationEntity> getOrganizationList(Integer startPage, Integer pageSize, String keyword) {

        QueryWrapper<OrganizationEntity> queryWrapper = new QueryWrapper<>();

        if (startPage == null || startPage <= 0) {
            startPage = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }

        if (!StringUtils.isEmpty(keyword)) {
            keyword = keyword.trim();
            queryWrapper.like("name", keyword);
        }

        Page<OrganizationEntity> page = new Page<>(startPage, pageSize);
        Page<OrganizationEntity> result = organizationDao.selectPage(page, queryWrapper);
        return result;

    }

    @Override
    @Transactional
    public void adminUpdateOrganization(AdminUpdateOrganizationBO bo) {
        QueryWrapper<OrganizationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", bo.getId());

        int orgExists = organizationDao.selectCount(queryWrapper);
        if (orgExists <= 0) {
            throw new MyException(404, "未找到机构");
        }

        String adminId = bo.getAdminId();
        OrganizationEntity updateEntity = new OrganizationEntity();
        updateEntity.setId(bo.getId());
        updateEntity.setName(bo.getName().trim());

        if (!StringUtils.isEmpty(adminId)) {
            int count = userDao.selectCount(
                    new QueryWrapper<UserEntity>()
                            .eq("id", adminId)
                            .eq("type", UserTypeConstant.OWNER)
            );
            if (count > 0) {
                updateEntity.setAdminId(bo.getAdminId());
            } else {
                throw new MyException(404, "未找到该管理员");
            }
        }

        organizationDao.updateById(updateEntity);
    }


}
